package com.ruoyi.system.wnjk.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class OrderService {

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    public String createOrder(String userId, LocalDate date, String period) {
        String orderId = "ORDER_" + UUID.randomUUID();
        String orderKey = "order:" + orderId;

        // 存储订单信息
        Map<String, String> orderData = new HashMap<>();
        orderData.put("userId", userId);
        orderData.put("date", date.toString());
        orderData.put("period", period);
        redisTemplate.opsForHash().putAll(orderKey, orderData);

        // 设置订单20分钟后过期（触发自动回滚）
        redisTemplate.expire(orderKey, 20, TimeUnit.MINUTES);
        return orderId;
    }
}